{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Buddy String\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/Z1cCt1nUR3s2fiItLKqWoA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given two string `A` and `B` of lowercase letters, return `True` if and only if we can swap two letters in `A` so that the result equals `B`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input: A = 'ab', B = 'ba'\n",
    "Output: True\n",
    "```\n",
    "\n",
    "```text\n",
    "Input: A = 'ab', B = 'ab'\n",
    "Output: False\n",
    "```\n",
    "\n",
    "```text\n",
    "Input: A = 'aa', B = 'aa'\n",
    "Output: True\n",
    "```\n",
    "\n",
    "```text\n",
    "Input: A = 'aaaaaaabc', B = 'aaaaaaacb'\n",
    "Output: True\n",
    "```\n",
    "\n",
    "```text\n",
    "Input: A = '', B = 'aa'\n",
    "Output: False\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def buddyStrings(self, A, B):\n",
    "        length_A = len(A)\n",
    "        length_B = len(B)\n",
    "        \n",
    "        # If and only if 2 strings have the same length,\n",
    "        # they can be Buddy String.\n",
    "        if length_A != length_B:\n",
    "            return False\n",
    "        \n",
    "        # If 2 strings are the same and there's a dulplicate pair,\n",
    "        # it must be a Buddy String.\n",
    "        elif A == B and len(set(A)) < length_A:\n",
    "            return True\n",
    "        \n",
    "        else:\n",
    "            # Create a dictionary to record conflicting characters.\n",
    "            diff = []\n",
    "            \n",
    "            # Push conflicting paris into the dictionary.\n",
    "            for i in range(length_A):\n",
    "                if A[i] != B[i]:\n",
    "                    diff.append((A[i], B[i]))\n",
    "                    \n",
    "            # If there're only 2 pairs\n",
    "            if len(diff) == 2:\n",
    "                return (\n",
    "                    diff[0][0] == diff[1][1] and\n",
    "                    diff[1][0] == diff[0][1]\n",
    "                )\n",
    "            \n",
    "            # Otherwise, it can't be a Buddy String.\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "print(solution.buddyStrings('aaaaaaabc', 'aaaaaaacb'))\n",
    "# True\n",
    "print(solution.buddyStrings('aaaaaabbc', 'aaaaaaacb'))\n",
    "# False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notes\n",
    "\n",
    "1. 2 strings are buddy strings if and only if their length are the same.\n",
    "\n",
    "2. Judge 2 strings if they are the same.\n",
    "\n",
    "3. If they are the same, judge if there are same characters we can exchange.\n",
    "\n",
    "4. If there are only 2 differences, and one can be operated to the other, they are buddy strings. Otherwise, they aren't."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
